/*
 * @Date: 2022-01-05 14:43:49
 * @LastEditors: LeeKing
 * @LastEditTime: 2022-01-05 15:00:08
 * @FilePath: \hmmm-20220102\src\utils\validate.js
 */
/**
 * Created by jiachenpan on 16/11/18.
 */

export function isvalidUsername (str) {
  const validMap = ['admin', 'editor']
  return validMap.indexOf(str.trim()) >= 0
}

/* 合法uri */
export function validateURL (textval) {
  const urlregex =
    /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/
  return urlregex.test(textval)
}

/* 小写字母 */
export function validateLowerCase (str) {
  const reg = /^[a-z]+$/
  return reg.test(str)
}

/* 大写字母 */
export function validateUpperCase (str) {
  const reg = /^[A-Z]+$/
  return reg.test(str)
}

/* 大小写字母 */
export function validatAlphabets (str) {
  const reg = /^[A-Za-z]+$/
  return reg.test(str)
}

/**
 * validate email
 * @param email
 * @returns {boolean}
 */
export function validateEmail (email) {
  // eslint-disable-next-line
  const re =
    /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  return re.test(email)
}

export function formatDate (v) {
  if (v) {
    // 将Date()对象转成YYYY-MM-DD HH:MM:SS格式
    const year = v.getFullYear()
    const month = v.getMonth() + 1 < 10 ? `0${v.getMonth() + 1}` : v.getMonth()
    const day = v.getDate() + 1 < 10 ? `0${v.getDate() + 1}` : v.getDate()
    const hour = v.getHours() < 10 ? `0${v.getHours()}` : v.getHours()
    const minute = v.getMinutes() < 10 ? `0${v.getMinutes()}` : v.getMinutes()
    const second = v.getSeconds() < 10 ? `0${v.getSeconds()}` : v.getSeconds()
    return `${year}-${month}-${day}   `
  }
  return ''
}
